'use strict';

import './css/index.css';
import "babel-polyfill";

import route from './Route';
// let school = require('./School');
import school from './School';
import stuClass from './StuClass';

//当前显示的页面
let currentShowView;
const indexHash = "#School";
const allRoute = [];
//新增路由
function addRoute(h) {
    if (allRoute.indexOf(h) != -1) {
        throw ("route重复:" + h.name);
    } else {
        let k = "#" + h.name;
        console.log("新增hash:" + k);
        //这里有个bug，假如2个一样名字的类在不同文件夹会有问题，先硬性规定不能有一样名字的类
        allRoute[k] = h;
    }
}
addRoute(school);
addRoute(stuClass);

$(async function () {
    const pathname = location.pathname;
    let hash = location.hash;
    console.log("pathname:", pathname, ",hash:", hash);
    if (!hash) {
        hash = indexHash;
        location.href = pathname + indexHash;
    }
    window.route = route;

    hashChangeBind();
});

//绑定hash改变事件
function hashChangeBind() {
    if (("onhashchange" in window) && ((typeof document.documentMode === "undefined") || document.documentMode == 8)) {
        // 浏览器支持onhashchange事件
        window.onhashchange = hashChangeFire;  // TODO，对应新的hash执行的操作函数
    } else {
        let oldHash = location.hash;
        function isHashChanged() {
            let nowHash = location.hash;
            if (oldHash != nowHash) {
                oldHash = nowHash;
                return true;
            } else {
                return false;
            }
        }
        // 不支持则用定时器检测的办法
        setInterval(function () {
            var ischanged = isHashChanged();  // TODO，检测hash值或其中某一段是否更改的函数
            if (ischanged) {
                hashChangeFire();  // TODO，对应新的hash执行的操作函数
            }
        }, 100);
    }
    function hashChangeFire() {
        console.log("hash产生了变化:" + location.hash);
        let allRoute2 = Object.keys(allRoute);
        if (allRoute2.indexOf(location.hash) != -1) {
            //页面初始化事件，每个页面都要重写这个函数
            let goRoute = allRoute[location.hash];
            pageInit(goRoute, location.hash);
        }
    };
}

/**
 * 加载子页面的dom
 * @param {*} goRoute 
 * @param {*} currentHash 
 */
function pageInit(goRoute, currentHash) {
    console.log("父页面初始化了");

    if (currentShowView) {
        $("#" + currentShowView).hide();
    }

    function pageInit2() {
        currentShowView = htmlFileName;
        $("#" + currentShowView).show();
        // 这个函数goRoute里面必须要有否则页面不执行任何代码
        goRoute.pageInit(currentHash, window.currentGoParam);
    }

    let htmlFileName = currentHash.substr(1);//去掉最前面的那个#
    let logicDiv = `<div id=${htmlFileName}></div>`;
    if ($("#" + htmlFileName).length == 0) {
        $.ajax({
            url: "/public/school/" + htmlFileName + ".html?v=" + new Date().getTime(),
            dataType: "html",
            type: "get",
            success: function (result) {
                $("#container").append(logicDiv);
                $("#" + htmlFileName).html(result);
                pageInit2();
            },
            error: function (a, b, c) {
                console.log("系统错误，请重试:" + c);
            }
        });
    } else {
        pageInit2();
    }
}